package com.dave.serviceribbon

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.boot.web.servlet.ServletRegistrationBean
import org.springframework.cloud.client.discovery.EnableDiscoveryClient
import org.springframework.cloud.client.loadbalancer.LoadBalanced
import org.springframework.cloud.netflix.hystrix.EnableHystrix
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.web.client.RestTemplate

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
@ComponentScan(basePackages = ["com.dave.serviceribbon"])
class ServiceRibbonApplication {

    val servlet: ServletRegistrationBean<*>
        @Bean
        get() {
            val streamServlet = HystrixMetricsStreamServlet()
            val registrationBean = ServletRegistrationBean(streamServlet)
            registrationBean.setLoadOnStartup(1)
            registrationBean.addUrlMappings("/hystrix.stream")
            registrationBean.setName("HystrixMetricsStreamServlet")
            registrationBean.setName("HystrixMetricsStreamServlet")
            return registrationBean

        }

    /**
     * restTemplate 负载调用两个实例
     * @return
     */
    @Bean
    @LoadBalanced
    internal fun restTemplate(): RestTemplate {
        return RestTemplate()
    }

    companion object {

        @JvmStatic
        fun main(args: Array<String>) {
            runApplication<ServiceRibbonApplication>(*args)
            //SpringApplication.run(ServiceRibbonApplication::class.java, *args)
        }
    }

}
